﻿namespace LiteBlog.Controllers.Helpers
{
    using LiteBlog.Common.Enums;
    using System.ServiceModel.Syndication;
    using System.Text;
    using System.Web.Mvc;
    using System.Xml;

  
    public class FeedActionResult : ActionResult
    {
        internal SyndicationFeed Feed { get; set; }
        public FeedType FeedType { get; set; }

        public FeedActionResult() { }

        internal FeedActionResult(SyndicationFeed feed, FeedType feedType)
        {
            Feed = feed;
            FeedType = feedType;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            using (var writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                switch (FeedType)
                {
                    case FeedType.ATOM:
                        Feed.SaveAsAtom10(writer);
                        context.HttpContext.Response.ContentType = "application/atom+xml";
                        break;
                    case FeedType.RSS:
                        Feed.SaveAsRss20(writer);
                        context.HttpContext.Response.ContentType = "application/rss+xml";
                        break;
                }
            }
        }
    }
}